﻿
using System.Collections;
using System.Xml;

namespace Xacml.Core.Policy
{
  public class RuleCollectionReadWrite : CollectionBase
  {
    public virtual int Add(RuleElementReadWrite value)
    {
      return this.List.Add((object) value);
    }

    public new virtual void Clear()
    {
      base.Clear();
    }

    public new virtual void RemoveAt(int index)
    {
      base.RemoveAt(index);
    }

    public int GetIndex(RuleElementReadWrite rule)
    {
      for (int index = 0; index < this.Count; ++index)
      {
        if (this.List[index] == rule)
          return index;
      }
      return -1;
    }

    public void WriteDocument(XmlWriter writer)
    {
      foreach (RuleElementReadWrite elementReadWrite in (CollectionBase) this)
      {
        writer.WriteStartElement("Rule");
        writer.WriteAttributeString("RuleId", elementReadWrite.Id);
        writer.WriteAttributeString("Effect", elementReadWrite.Effect.ToString());
        if (!elementReadWrite.Description.Equals(string.Empty))
          writer.WriteElementString("Description", elementReadWrite.Description);
        if (elementReadWrite.Target != null)
          elementReadWrite.Target.WriteDocument(writer);
        if (elementReadWrite.Condition != null)
          elementReadWrite.Condition.WriteDocument(writer);
        writer.WriteEndElement();
      }
    }
  }
}
